import { useState, useEffect } from 'react'
import { Button, Space, Divider, Input, message } from 'antd'
import s from "./index.module.less";

function App({
  configF
}: any): JSX.Element {

  const [config, setConfig] = useState<any>({
    AutoRunRobot: '',
    RobotManage: '',
    npcText: '',
    hcQf: '',
    hcQm: '',
  })

  const getVal = async () => {
    if (!configF.path2) {
      return message.error('请输入会员站配置文件的路径')
    }

    const c = {
      ...config
    }

    c.AutoRunRobot = await window.api.getVal(window.api.getLocalMirserverPath(), '攻沙脚本', '攻沙脚本全套', 'AutoRunRobot.txt')
    c.RobotManage = await window.api.getVal(window.api.getLocalMirserverPath(), '攻沙脚本', '攻沙脚本全套', 'RobotManage.txt')
    c.npcText = await window.api.getVal(window.api.getLocalMirserverPath(), '攻沙脚本', '攻沙脚本全套', 'NPC.txt')
    c.hcQf = await window.api.getVal(window.api.getLocalMirserverPath(), '攻沙脚本', '9点攻沙拉回城通用脚本', '攻击触发范例①-不卡-推荐使用【限esp-B版使用】', 'QFunction-0.txt')
    c.hcQm = await window.api.getVal(window.api.getLocalMirserverPath(), '攻沙脚本', '9点攻沙拉回城通用脚本', '攻击触发范例①-不卡-推荐使用【限esp-B版使用】', 'QManage.txt')
    setConfig(c)

    return c
  }

  useEffect(() => {
    getVal()
  }, [])

  return (
    <div className={s.gongsha}>
      <Divider>攻沙</Divider>

      <div>
        <p>
          要复制到AutoRunRobot的攻沙内容:

          <Button size='small' type='primary' onClick={async () => {
            window.api.copePath(config.AutoRunRobot)
            window.api.openFile(configF, 'Robot_def', 'AutoRunRobot.txt')
          }}>点击自动复制内容并打开AutoRunRobot.txt</Button>
        </p>
        <div className='flex'>
          <Input.TextArea
            spellCheck="false"
            autoSize
            value={config.AutoRunRobot}
            onChange={(e) => {
              const c = {
                ...config
              }
              c.AutoRunRobot = e.target.value
              setConfig(c)
            }}
          />
        </div>
      </div>

      <div>
        <p>
          要复制到RobotManage的攻沙内容:

          <Button size='small' type='primary' onClick={async () => {
            window.api.copePath(config.RobotManage)
            window.api.openFile(configF, 'Robot_def', 'RobotManage.txt')
          }}>点击自动复制内容并打开RobotManage.txt</Button>
        </p>
        <div className='flex'>
          <Input.TextArea
            spellCheck="false"
            autoSize={{ maxRows: 15 }}
            value={config.RobotManage}
            onChange={(e) => {
              const c = {
                ...config
              }
              c.RobotManage = e.target.value
              setConfig(c)
            }}
          />
        </div>
      </div>

      <div>
        <p>
          添加攻沙NPC(<span style={{ color: 'red' }}>里面的‘主城代码’自动替换上面输入的主城代码，根据需要自己手动修改，需要替换代码中原有的攻沙npc</span>):
          <br />
          <Button size='small' type='primary' onClick={async () => {
            window.api.copePath(config.npcText)
            await window.api.huiyuan2({ ...configF, ...config })
          }}>点击自动复制内容并打开Merchant.txt</Button>
          <Button size='small' type='primary' style={{ marginLeft: 10 }}
          onClick={async () => {
            window.api.openMirserver('攻沙脚本', '攻沙脚本全套')
          }}>打开文件</Button>
        </p>
        <div className='flex'>
          <Input.TextArea
            spellCheck="false"
            autoSize
            value={config.npcText}
            onChange={(e) => {
              const c = {
                ...config
              }
              c.npcText = e.target.value
              setConfig(c)
            }}
          />
        </div>
      </div>

      <Space>
        <Button size='small' type='primary' onClick={async () => {
          const isTure = await window.api.gongsha(configF)
          if (isTure) {
            message.success('修改成功,关闭弹窗自动打开沙城领奖员,请手动修改攻沙奖励')
            setTimeout(() => {
              window.api.openFile(configF, 'Market_Def', `沙城领奖员-${configF.zhucheng_code}.txt`)
            }, 1000)
          } else {
            message.error('请填写完整')
          }
        }}>复制攻城传送和领奖员到Market_Def</Button>
      </Space>

      <div>
        <p>
          开启攻沙拉回城:

          <Button size='small' type='primary' onClick={async () => {
            window.api.copePath(config.hcQf)
            window.api.openFile(configF, 'Market_Def', 'QFunction-0.txt')
          }}>点击自动复制内容并打开qf</Button>
        </p>
        <div className='flex'>
          <Input.TextArea
            spellCheck="false"
            autoSize={{ maxRows: 15 }}
            value={config.hcQf}
            onChange={(e) => {
              const c = {
                ...config
              }
              c.hcQf = e.target.value
              setConfig(c)
            }}
          />
        </div>
      </div>

      <div>
        <Button size='small' type='primary' onClick={async () => {
          window.api.copePath(config.hcQm)
          window.api.openFile(configF, 'MapQuest_def', 'QManage.txt')
        }}>点击自动复制内容并打开qm</Button>
        <br />
        <Input.TextArea
          spellCheck="false"
          autoSize
          value={config.hcQm}
          onChange={(e) => {
            const c = {
              ...config
            }
            c.hcQf = e.target.value
            setConfig(c)
          }}
        />
      </div>
    </div>
  )
}

export default App
